package com.lzy.webapi.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import com.lzy.framework.tools.date.DateStyle;
import com.lzy.framework.tools.date.DateUtils;
import com.lzy.product.common.util.CalendarUtil;
import com.lzy.product.constants.profit.ProfitTargetEnum;
import com.lzy.product.dtos.statistics.DayProfitParamDto;
import com.lzy.product.dtos.statistics.DayProfitVoDto;
import com.lzy.product.dtos.statistics.OrderProfitRecordDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.StatisticsFacade;
import com.lzy.webapi.request.statistics.OrderProfitRecordRequest;
import com.lzy.webapi.response.statistics.DayProfitResponse;
import com.lzy.webapi.response.statistics.OrderProfitRecordResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 统计管理
 * Created by WIN7 on 2017/12/13.
 */
@Controller
@RequestMapping("api/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsFacade statisticsFacade;

    /**
     * 查询当月的日收益列表
     *
     * @param token
     * @param yearMonth 月份，格式：yyyy-MM
     * @return
     */
    @RequestMapping(value = "/dayProfitList", method = RequestMethod.GET)
    public @ResponseBody
    Result getDayProfitList(@RequestHeader("token") String token, @RequestParam("yearMonth") String yearMonth) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        DayProfitParamDto paramDto = new DayProfitParamDto();
        paramDto.setUserId(ut.getUserId());

        if (yearMonth != null) {
            //检查参数格式
            if (yearMonth.matches("(\\d+){4}-(\\d+){2}")) {
                //查询起始日期 当月的第一天
                String startDate = yearMonth + "-01";
                paramDto.setStartDate(startDate);
                //查询结束日期 当月的最后一天
                int year = Integer.valueOf(yearMonth.substring(0, 4)).intValue();
                int month = Integer.valueOf(yearMonth.substring(5, 7)).intValue();
                String endDate = DateUtils.DateToString(CalendarUtil.getLastDayOfMonth(year, month), DateStyle.YYYY_MM_DD);
                paramDto.setEndDate(endDate);

                List<DayProfitVoDto> list = statisticsFacade.queryDayProfitListNoPage(paramDto);
                if (list != null) {
                    List<DayProfitResponse> responseList = new ArrayList<>();
                    for (DayProfitVoDto voDto : list) {
                        DayProfitResponse response = new DayProfitResponse();
                        response.setDate(voDto.getDate());
                        response.setCount(voDto.getCount());
                        response.setProfit(BigDecimalUtil.to2Point(voDto.getProfit()).toString());
                        responseList.add(response);
                    }
                    return new Result(responseList);
                }
            }
        }
        return new Result();
    }

    /**
     * 查询当天的收益列表
     *
     * @param token
     * @param request
     * @return
     */
    @RequestMapping(value = "/profitRecordList", method = RequestMethod.GET)
    public @ResponseBody
    Result getProfitRecordList(
            @RequestHeader("token") String token,
            OrderProfitRecordRequest request) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        OrderProfitRecordDto paramDto = DozerUtils.map(request, OrderProfitRecordDto.class);
        paramDto.setUserId(ut.getUserId());
        PageInfo<OrderProfitRecordDto> pageInfo = statisticsFacade.queryRecordList(pager, paramDto);
        if (pageInfo != null && pageInfo.getList() != null) {
            List<OrderProfitRecordResponse> responseList = new ArrayList<>();
            for (OrderProfitRecordDto dto : pageInfo.getList()) {
                OrderProfitRecordResponse response = new OrderProfitRecordResponse();
                if (ProfitTargetEnum.ONE_LEVEL.getCode().equals(request.getLevel())) {
                    response.setOrderId(dto.getOrderId());
                } else {
                    response.setOrderUserName(dto.getOrderUserName());
                }
                response.setSkuName(dto.getSkuName());
                response.setProfit("+" + dto.getProfit());
                response.setOrderTime(dto.getOrderTime());
                responseList.add(response);
            }
            return new Result(pageInfo.getList());
        }
        return new Result();
    }
}
